package com.wannengbang.cloudleader.shop;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.ShopDetailsBean;
import com.wannengbang.cloudleader.bean.ShopListBean;
import com.wannengbang.cloudleader.shop.model.IShopModel;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopGoodsDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_add_cart)
    TextView tvAddCart;
    @BindView(R.id.tv_now_buy)
    TextView tvNowBuy;
    @BindView(R.id.iv_goods_thumb)
    ImageView ivGoodsThumb;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_integral_price)
    TextView tvIntegralPrice;
    @BindView(R.id.tv_market_price)
    TextView tvMarketPrice;
    @BindView(R.id.tv_sales)
    TextView tvSales;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_freight_money)
    TextView tvFreightMoney;
    private String goods_id;
    private IShopModel shopModel;
    private ShopListBean.DataBean.ItemListBean itemListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_details);
        ButterKnife.bind(this);
        initView();
        requestGoodsShow();
    }

    public void initView() {
        shopModel = new ShopModelImpl();
        goods_id = getIntent().getStringExtra("goods_id");
    }

    public void requestGoodsShow() {
        ViewLoading.showProgress(mActivity, "加载中......");
        shopModel.requestIntegralGoodsShow(goods_id, new DataCallBack<ShopDetailsBean>() {
            @Override
            public void onSuccessful(ShopDetailsBean bean) {
                if (bean.getData() != null) {
                    bean.getData().setGoods_id(bean.getData().getId());
                    itemListBean = bean.getData();
                    GlideUtils.loadImage(mActivity, ivGoodsThumb, bean.getData().getThumb());
                    tvIntegralPrice.setText(bean.getData().getIntegral_price());
                    tvTitle.setText(bean.getData().getName());
                    tvDesc.setText(bean.getData().getDesc());
                    String money = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getMarket_price()) / 100);
                    tvMarketPrice.setText("市场价：" + money + "元");
                    tvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                    tvSales.setText("已兑" + bean.getData().getSales());
                    String freight = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getFreight()) / 100);
                    tvFreightMoney.setText("运费：" + freight + "元");
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_add_cart, R.id.tv_now_buy})
    public void onViewClicked(View view) {
        if (itemListBean == null) {
            return;
        }
        switch (view.getId()) {
//            case R.id.tv_add_cart:
//                ShopCartDialog dialog = ShopCartDialog.newInstance(1, itemListBean);
//                dialog.show(getSupportFragmentManager(), "dialog");
//                break;
            case R.id.tv_now_buy:
                List<ShopListBean.DataBean.ItemListBean> beanList = new ArrayList<>();
                beanList.add(itemListBean);
                Intent intent = new Intent(mActivity, ShopConfirmOrderActivity.class);
                intent.putExtra("itemList", (Serializable) beanList);
                startActivity(intent);
                break;
        }
    }
}
